/*
*  Copyright 2019-2020 gaosc
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.admin.qujiang.service.impl;

import com.admin.qujiang.model.YlStreetCountModel;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.admin.qujiang.service.IYlStreetCountService;
import com.admin.qujiang.dto.YlStreetCountDto;
import com.admin.qujiang.service.dto.YlStreetCountQueryCriteria;
import com.admin.qujiang.mapper.YlStreetCountMapper;
import com.admin.utils.DozerUtils;
import org.dozer.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.*;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author gaosc
* @date 2021-12-07
**/
@Service
@RequiredArgsConstructor
public class YlStreetCountServiceImpl extends ServiceImpl<YlStreetCountMapper, YlStreetCountModel> implements IYlStreetCountService {

    // dozer做Model与DTO的转换
    private final Mapper mapper;
    
    private QueryWrapper<YlStreetCountModel> buildWrapper(YlStreetCountQueryCriteria criteria, Boolean isQuery) {
        QueryWrapper<YlStreetCountModel> query = null;
        if (null != criteria) {
            query = new QueryWrapper<YlStreetCountModel>();
        }
        return query;
    }

    @Override
    public List<YlStreetCountDto> queryAll(YlStreetCountQueryCriteria criteria, Boolean isQuery){
        List<YlStreetCountModel> ylStreetCounts = this.list(buildWrapper(criteria, isQuery));
        return DozerUtils.mapList(mapper, ylStreetCounts, YlStreetCountDto.class);
    }

    @Override
    @Transactional
    public YlStreetCountDto findById(Integer id) {
        YlStreetCountModel ylStreetCount = Optional.ofNullable(this.getById(id)).orElseGet(YlStreetCountModel::new);
        ValidationUtil.isNull(ylStreetCount.getId(),"YlStreetCount","id",id);
        return mapper.map(ylStreetCount, YlStreetCountDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YlStreetCountModel create(YlStreetCountModel resources) {
        this.save(resources);
        return resources;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YlStreetCountModel resources) {
        YlStreetCountModel ylStreetCount = Optional.ofNullable(this.getById(resources.getId())).orElseGet(YlStreetCountModel::new);
        ValidationUtil.isNull(ylStreetCount.getId(), "YlStreetCount", "id", resources.getId());
        ylStreetCount = mapper.map(resources, YlStreetCountModel.class);
        this.saveOrUpdate(ylStreetCount);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public void download(List<YlStreetCountDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YlStreetCountDto ylStreetCount : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("镇街名称", ylStreetCount.getVillage());
            map.put("收件量", ylStreetCount.getReceivingCount());
            map.put("六合一公用系统办件量", ylStreetCount.getPublicCount());
            map.put("数据时间", ylStreetCount.getDataTime());
            map.put("创建时间", ylStreetCount.getCreateTime());
            map.put("修改时间", ylStreetCount.getUpdateTime());
            map.put("创建人", ylStreetCount.getCreateBy());
            map.put("修改人", ylStreetCount.getUpdateBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}